/*
 * @Author: WangLi
 * @Date: 2021-07-15 08:08:20
 * @LastEditors: WangLi
 * @LastEditTime: 2024-03-10 09:52:42
 * @Description:
 */
import { isPromise } from "./validator";
export const formatNumber = (n) => {
  n = n.toString();
  return n[1] ? n : `0${n}`;
};

/**
 * 判断邮箱
 * @param {*} val
 */
export const isEmail = function (val) {
  const reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/g;
  return reg.test(val);
};

/**
 * 判断手机号
 * @param {*} val
 */
export const isPhone = function (val) {
  const reg = /^1\d{10}/g;
  return reg.test(val);
};

// 数组去重
export const unique = function (arr, key) {
  let obj = {};
  arr = arr.reduce(function (item, next) {
    obj[next[key]] ? "" : (obj[next[key]] = true && item.push(next));
    return item;
  }, []);
  return arr;
};

let systemInfo;
export function getSystemInfoSync() {
  if (systemInfo == null) {
    systemInfo = uni.getSystemInfoSync();
  }
  return systemInfo;
}

export function range(num, min, max) {
  return Math.min(Math.max(num, min), max);
}

export function isObj(x) {
  const type = typeof x;
  return x !== null && (type === "object" || type === "function");
}

export function requestAnimationFrame(cb) {
  return cb();
  const systemInfo = getSystemInfoSync();

  if (systemInfo.platform === "devtools") {
    return setTimeout(() => {
      cb();
    }, 1000 / 30);
  }

  return uni
    .createSelectorQuery()
    .selectViewport()
    .boundingClientRect()
    .exec(() => {
      cb();
    });
}

export const computData = function () {
  let startDate = new Date();
  let endDate = new Date();
  let year = startDate.getFullYear();
  endDate.setDate(startDate.getDate() + 30);
  let dataArr = [];
  let weeks = ["日", "一", "二", "三", "四", "五", "六"];
  while (endDate.getTime() - startDate.getTime() >= 0) {
    let month =
      (startDate.getMonth() + 1).toString().length == 1
        ? "0" + (startDate.getMonth() + 1).toString()
        : startDate.getMonth() + 1;
    let day =
      startDate.getDate().toString().length == 1
        ? "0" + startDate.getDate()
        : startDate.getDate();
    let week = weeks[startDate.getDay()];
    dataArr.push({
      text: `${month}-${day} 周${week}`,
      value: `${year}-${month}-${day}`,
    });
    startDate.setDate(startDate.getDate() + 1);
  }
  dataArr[0].text = `${dataArr[0].text} (今天)`;
  dataArr[1].text = `${dataArr[1].text} (明天)`;
  return dataArr;
};

// 防抖
export const debounce = function (fn, wait) {
  let timeout;
  return function () {
    let context = this;
    let args = arguments;

    if (timeout) clearTimeout(timeout);

    let callNow = !timeout;
    timeout = setTimeout(() => {
      timeout = null;
    }, wait);

    if (callNow) fn.apply(context, args);
  };
};

export const toPromise = function (promiseLike) {
  if (isPromise(promiseLike)) {
    return promiseLike;
  }

  return Promise.resolve(promiseLike);
};

export const sleep = async function (delay) {
  return new Promise((resolve) => setTimeout(resolve, delay));
};

export const rpx2px = function (rpx) {
  const { windowWidth } = uni.getSystemInfoSync();
  return parseInt((windowWidth / 750) * rpx);
};

export function guid() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0,
      v = c == "x" ? r : (r & 0x3) | 0x8;
    return v.toString(16);
  });
}
